<html>
<head>
	<meta charset='utf-8' />
	<title>小区信息助手后台</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#content,.nav_div{width:98%;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#content,.nav_div{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>
<body>
<div id='content' style='position:relative;border:0px solid'>
	<h1 style='text-align:center'>城市开通管理</h1>
	
	<div style='text-align:center'>
		<label><input type="radio" name='open_type' value='1' /> 全部城市</label>　
		<label><input type="radio" name='open_type' value='2' /> 已开通城市</label>　
		<label><input type="radio" name='open_type' value='3' /> 未开通城市</label>
	</div>

	<div style='text-align:center;margin:20px 0;display:flex'>
		<select id="province_sel" style='flex:1'></select>
		<select id="city_sel" style='flex:1'></select>
		<select id="county_sel" style='flex:1'></select>
	</div>

	<div style='text-align:center'>
		<div style='margin:10px 0'>
			库地址：<input type="text" id='db_host'>
		</div>
		<div style='margin:10px 0'>
			库　名：<input type="text" id='db_name'>
		</div>

		<button id='set_open_city'>提交</button>
	</div>


</div>

</body>
</html>
<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<script>
$(function(){

	//设置提交按钮
	$('#set_open_city').click(function(){
		var db_host = $('#db_host').val().trim();
		var db_name = $('#db_name').val().trim();
		var city_id = $('#county_sel').find('option:selected').val();
		var data = {
			type:'set_open_city',
			db_host:db_host,
			db_name:db_name,
			city_id:city_id
		}
		Ajax2(data,function(res){
			switch(res){
				case 'err1':A('缺少参数！');break;
				case 'ok':
					A('操作成功！');
					sessionStorage.city_info = '';
					location.reload();
					break;
			}
		});
	});

	//类型保持
	$("input[name=open_type]").each(function(i,o){
		if(o.value == sessionStorage.open_type){
			o.checked = true;
		}
	});

	//开通类型转换
	$('input[name=open_type]').change(function(){
		sessionStorage.open_type = $(this).val();
		location.reload();
	});


	//获取全部城市信息
	GetCityInfo();
	function GetCityInfo(){
		if(!sessionStorage.city_info){
			Ajax2({type:'get_city_data'},function(res){
				if(!/^\[.+\]$/.test(res)){
					return;
				}
				C('from after');
				sessionStorage.city_info = res;
				ParseCity2();	//解析城市数据
			});
		}else{
			C('from sess');
			ParseCity2();	//解析城市数据
		}
	}

	function ParseCity2(){
		if(!sessionStorage.open_type){
			sessionStorage.open_type = 1;
		}
		ParseCity(sessionStorage.open_type);
	}


	/*
		### 解析城市数据
		参一：type 	int  	1:全部城市，2：已开通城市，3：未开通城市
	*/
	function ParseCity(type){
		if(!sessionStorage.city_info){
			return;
		}
		var data = JSON.parse(sessionStorage.city_info);
		//顺序 [0]id , [1]parent_id , [2]level , [3]code , [4]name , [5]db_host , [6]db_name

		//筛出省,市，县
		var province = [];	//省
		var city = [];		//市
		var county = [];	//县

		for(var i=0;i<data.length;i++){
			if(data[i][2] == 0){	
				 province.push(data[i]);
			}
			if(data[i][2] == 1){	
				 city.push(data[i]);
			}
			if(data[i][2] == 2){	//归纳县
				if(type == 1){
				 	county.push(data[i]);
				}
				if(type == 2 && data[i][5].length > 1 && data[i][6].length > 1){
				 	county.push(data[i]);
				}
				if(type == 3 && (data[i][5].length<1 || data[i][6].length<1)){
				 	county.push(data[i]);
				}
			}
		}

		//装载县到市
		for(var i=0;i<city.length;i++){
			var tmp_countys = [];
			for(var j=0;j<county.length;j++){
				if(county[j][1] == city[i][0]){
					tmp_countys.push(county[j]);
				}
			}
			city[i][7] = tmp_countys
		}
		
		//筛选空市
		var city2 = [];	
		for(var i=0;i<city.length;i++){
			if(city[i][7].length > 0){
				city2.push(city[i]);
			}
		}

		//装载市到省
		for(var i=0;i<province.length;i++){
			var tmp_citys = [];
			for(var j=0;j<city2.length;j++){
				if(city2[j][1] == province[i][0]){
					tmp_citys.push(city2[j]);
				}
			}
			province[i][7] = tmp_citys;
		}

		
		//筛选空省
		var province2 = [];	
		for(var i=0;i<province.length;i++){
			if(province[i][7].length > 0){
				province2.push(province[i]);
			}
		}

		ViewProvince(province2);
	}

	
	//显示省数据
	function ViewProvince(province2){
		var province2_str = '';
		for(var i=0;i<province2.length;i++){
			if(sessionStorage.province_index == i){
				province2_str += "<option selected value="+province2[i][0]+" >"+province2[i][4]+"</option>";
			}else{
				province2_str += "<option value="+province2[i][0]+" >"+province2[i][4]+"</option>";
			}
		}
		$('#province_sel').html(province2_str)

		var province_id = $('#province_sel').find('option:selected').val();

		//提取省下的市
		var city3 = '';
		for(var i=0;i<province2.length;i++){
			if(province2[i][0] == province_id){
				city3 = province2[i][7];
			}
		}

		$('#province_sel').change(function(){
			sessionStorage.province_index = $(this).find('option:selected').index();
			location.reload();			
		});
		ViewCity(city3);	//显示市
	
	}

	//显示省数据
	function ViewCity(city3){
		var city3_str = '';
		for(var i=0;i<city3.length;i++){
			if(sessionStorage.city_index == i){
				city3_str += "<option selected value="+city3[i][0]+" >"+city3[i][4]+"</option>";
			}else{
				city3_str += "<option value="+city3[i][0]+" >"+city3[i][4]+"</option>";
			}
		}
		$('#city_sel').html(city3_str)

		var city_id = $('#city_sel').find('option:selected').val();

		//提取市下的县
		var county3 = '';
		for(var i=0;i<city3.length;i++){
			if(city3[i][0] == city_id){
				county3 = city3[i][7];
			}
		}

		$('#city_sel').change(function(){
			sessionStorage.city_index = $(this).find('option:selected').index();
			location.reload();	
		});
		ViewCounty(county3);	//显示市
	}


	//显示县数据
	function ViewCounty(county3){
		var county3_str = '';
		for(var i=0;i<county3.length;i++){
			if(sessionStorage.county_index == i){
				county3_str += "<option selected value="+county3[i][0]+" >"+county3[i][4]+"</option>";
			}else{
				county3_str += "<option value="+county3[i][0]+" >"+county3[i][4]+"</option>";
			}
		}
		$('#county_sel').html(county3_str)


		$('#county_sel').change(function(){
			sessionStorage.county_index = $(this).find('option:selected').index();
			location.reload();	
		});

		var county_id = $('#county_sel').find('option:selected').val();
		for(var i=0;i<county3.length;i++){
			if(county3[i][0] == county_id){
				$('#db_name').val(county3[i][6]);
				$('#db_host').val(county3[i][5]);
			}
		}
	}





	//底部导航与显示页面
	$.ajax({
		url:'after_template_bottom_nav.html',
		success(res){
			$('body').append(res);
		}
	});
});
</script>